﻿using System.Windows;
using Bookvar.ApplicationModel;
using Bookvar.ServiceModel;
using BookvarWpf.Properties;
using LocalizationResources = Bookvar.UI.Localization.Resources;

namespace BookvarWpf.Windows
{
    /// <summary>
    /// Interaction logic for <see cref="LiveImagesWindow"/>
    /// </summary>
    public partial class OptionsWindow
    {
        private const string BulgarianCultureString = "bg";
        private const string EnglishCultureString = "en";

        public OptionsWindow()
        {
            InitializeComponent();

            tbSiteAddress.Text = Settings.Default.SiteAddress;
            tbUsername.Text = Settings.Default.Username;
            tbPassword.Text = Settings.Default.Password;
            tbDomain.Text = Settings.Default.Domain;
            chkCheckForBetaUpdates.IsChecked = Settings.Default.AutoCheckForBetaUpdates &&
                                               Settings.Default.AutoCheckForUpdates;
            chkCheckForReleaseUpdates.IsChecked = Settings.Default.AutoCheckForUpdates;
            nudAutoSaveMinutes.Value = Settings.Default.AutoRecoveryInterval;
            LoadCurrentLanguage();
        }

        public static ILocalizationService LocalizationService
        {
            get { return BookvarApplication.Current.ServiceLayer.GetService<ILocalizationService>(); }
        }

        private void LoadCurrentLanguage()
        {
            if ( Settings.Default.Language == EnglishCultureString )
            {
                languageComboBox.SelectedItem = languageComboBoxEnglish;
            }
            else if ( Settings.Default.Language == BulgarianCultureString )
            {
                languageComboBox.SelectedItem = languageComboBoxBulgarian;
            }
        }

        private void SaveClicked( object sender, RoutedEventArgs args )
        {
            Settings.Default.SiteAddress = tbSiteAddress.Text.Trim();
            Settings.Default.Username = tbUsername.Text.Trim();
            Settings.Default.Password = tbPassword.Text.Trim();
            Settings.Default.Domain = tbDomain.Text.Trim();
            Settings.Default.AutoCheckForUpdates = chkCheckForReleaseUpdates.IsChecked ?? false;

            Settings.Default.AutoCheckForBetaUpdates = ( chkCheckForBetaUpdates.IsChecked ?? false ) &&
                                                       ( chkCheckForReleaseUpdates.IsChecked ?? false );
            Settings.Default.AutoRecoveryInterval = (int) nudAutoSaveMinutes.Value;
            SaveSelectedLanguage();
            Settings.Default.Save();
            CloseCommand.Execute( null, this );
        }

        private void SaveSelectedLanguage()
        {
            string newLanguage = string.Empty;

            if ( languageComboBox.SelectedItem == languageComboBoxEnglish )
            {
                newLanguage = EnglishCultureString;
            }
            else if ( languageComboBox.SelectedItem == languageComboBoxBulgarian )
            {
                newLanguage = BulgarianCultureString;
            }

            if ( !string.IsNullOrEmpty( newLanguage ) )
            {
                if ( newLanguage != Settings.Default.Language )
                {
                    Settings.Default.Language = newLanguage;
                    MessageBox.Show( LocalizationResources.OptionsNewLanguageRestartMessage );
                }
            }
        }
    }
}